package model

import (
	"gorm.io/gorm"
	"seckill-srv/basic/config"
	"time"
)

type Activity struct {
	gorm.Model
	GoodsId           int       `json:"goods_id" gorm:"type:int;comment:'商品id'"`
	ActivityTitle     string    `json:"activity_title" gorm:"type:varchar(50);comment:'活动名称'"`
	ActivityDesc      string    `json:"activity_desc" gorm:"type:varchar(200);comment:'活动描述'"`
	ActivityPrice     float64   `json:"activity_price" gorm:"type:float;comment:'活动价格'"`
	ActivityStock     int       `json:"activity_stock" gorm:"type:int;comment:'活动库存'"`
	ActivityStartTime time.Time `json:"activity_start_time" gorm:"type:datetime;comment:'活动开始时间'"`
	ActivityEndTime   time.Time `json:"activity_end_time" gorm:"type:datetime;comment:'活动结束时间'"`
	ActivityStatus    int       `json:"activity_status" gorm:"type:tinyint;comment:'活动状态 1-待开始 2-进行中 3-已结束'"`
}

func (a *Activity) TableName() string {
	return "activity"
}

// 添加活动
func (a *Activity) AddActivity(tx *gorm.DB) error {
	return tx.Create(&a).Error
}

// 根据id查询活动
func (a *Activity) GetActivityById(activityId int) error {
	return config.DB.Model(&Activity{}).Where("id = ?", activityId).Limit(1).Find(&a).Error
}

// 更新活动状态
func (a *Activity) UpdateActivityStatus(tx *gorm.DB, activityId int) error {
	return tx.Model(&Activity{}).Where("id = ?", activityId).Updates(&a).Error
}
